home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / wstr.exe / WSTR.H < prev   
Text File  |  1993-02-25  |  11KB  |  271 lines

  1. #ifndef WStrIncluded
  2. #define WStrIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. // voice phone:  (406)543-7543
  8. // modem phone:  (406)543-1144 (2400N81)
  9. //  CompuServe:  72707,207
  10. //    Internet:  72707.207@CompuServe.com
  11.  
  12. #include <string.h>
  13. #include <WMisc.h>
  14.  
  15. #ifdef MAJORBBS
  16.   #include <stdlib.h>
  17. #endif
  18.  
  19. #define DefaultStringExtra    15
  20.   /*  used internally by "String". Since most strings are very small yet
  21.   will have a little bit more added to them, this extra allocation should
  22.   save quite a bit of time.  The only time it would be a hendrance is when
  23.   an array of strings is created */
  24.  
  25. class String;
  26. class StackString;
  27. class String40;
  28. class String120;
  29.  
  30. class BaseString
  31.   {
  32.     protected:
  33.       char* P;   // character string
  34.       int Len;   // Length of string, excluding null character
  35.       int Alloc; // amount of actual storage allocated (including null char)
  36.       friend String;
  37.       friend StackString;
  38.       friend String40;
  39.       friend String120;
  40.     public:
  41.       operator const char*() const {return P;}
  42.       char& operator[](int Index);
  43.       Bool operator<(const BaseString& S) const { return strcmp(P, S.P) < 0; }
  44.       Bool operator>(const BaseString& S) const { return strcmp(P, S.P) > 0; }
  45.       Bool operator<=(const BaseString& S) const { return strcmp(P, S.P) <= 0; }
  46.       Bool operator>=(const BaseString& S) const { return strcmp(P, S.P) >= 0; }
  47.       Bool operator==(const BaseString& S) const;
  48.       Bool operator!=(const BaseString& S) const { return !(*this==S); }
  49.  
  50.       Bool operator<(const char* CS)  const { return strcmp(P,CS) < 0; }
  51.       Bool operator>(const char* CS)  const { return strcmp(P,CS) > 0; }
  52.       Bool operator<=(const char* CS) const { return strcmp(P,CS) <= 0; }
  53.       Bool operator>=(const char* CS) const { return strcmp(P,CS) >= 0; }
  54.       Bool operator==(const char* CS) const { return strcmp(P,CS) == 0; }
  55.       Bool operator!=(const char* CS) const { return strcmp(P,CS) != 0; }
  56.  
  57.       friend Bool operator<(const char* cs, const BaseString& S)
  58.         { return strcmp(cs, (S.P)) < 0; }
  59.       friend Bool operator>(const char* cs, const BaseString& S)
  60.         { return strcmp(cs, (S.P)) > 0; }
  61.       friend Bool operator<=(const char* cs, const BaseString& S)
  62.         { return strcmp(cs, (S.P)) <= 0; }
  63.       friend Bool operator>=(const char* cs, const BaseString& S)
  64.         { return strcmp(cs, (S.P)) >= 0; }
  65.       friend Bool operator==(const char* cs, const BaseString& S)
  66.         { return strcmp(cs, (S.P)) == 0; }
  67.       friend Bool operator!=(const char* cs, const BaseString& S)
  68.         { return strcmp(cs, (S.P)) != 0; }
  69.  
  70.       int Length() const { return Len; }
  71.       void ToLower();  // force all of StackString to lower case
  72.       void ToUpper();  // force all of StackString to upper case
  73.       int Capacity() const { return Alloc - 1; }
  74.       int Size() const { return (Len+2); } // the current size to store
  75.       int Index(char SearchChar, int StartIndex=0) const;
  76.       int Index(const char* SearchStr, int StartIndex=0) const;
  77.       Bool Find(char SearchChar, int StartIndex=0) const
  78.           {return (Index(SearchChar,StartIndex)!=NotFound);}
  79.       Bool Find(const char* SearchStr, int StartIndex=0) const
  80.           {return (Index(SearchStr,StartIndex)!=NotFound);}
  81.       int Count(char C) const;  // how many of this character occur
  82.  
  83.       void Delete(int Index=0,int Length=1);
  84.       void DeleteLast();
  85.       void Trim();  //   chop off leading and trailing spaces
  86.       void TrimLead();
  87.       void TrimTrail();
  88.       void TrimTrailTo(char C);
  89.         // remove last chars until C is found, then remove C too!
  90.  
  91.       char At(int Index) const;
  92.       char operator()(int Index) const {return At(Index);}
  93.       char Last() const; // the last char in the string
  94.       #ifdef MAJORBBS
  95.         void* operator new(size_t size){return malloc(size);}
  96.         void  operator delete(void* p) {free(p);}
  97.       #endif
  98.   };
  99.  
  100. class String: public BaseString
  101.   {
  102.     protected:
  103.       void ReNew(int NewCapacity);
  104.       void New(); // Uses Alloc
  105.     public:
  106.       String(const char& C, int L=1, int Extra=DefaultStringExtra);
  107.         // String S(' ',20) constructs a string consisting of 20 spaces
  108.       String(int Extra=DefaultStringExtra);
  109.         // constructor based on size desired or size not yet known
  110.       String(const char*, int Extra=DefaultStringExtra);
  111.         // a char* type string is used to construct a String type string
  112.       String(const BaseString&, int Extra=DefaultStringExtra);
  113.         // creating one String from another
  114.       #ifdef MAJORBBS
  115.         ~String() {free(P);}
  116.       #else
  117.         ~String() {delete P;}
  118.       #endif
  119.  
  120.       // assignment operators
  121.       void operator=(const BaseString&);
  122.       void operator=(const char*);
  123.       void operator=(const char);
  124.  
  125.       // String concatination (S1=S2+S3)
  126.       // String operator+(const StackString& S) const;
  127.       String operator+(const String40& S) const;
  128.       String operator+(const String120& S) const;
  129.       String operator+(const String&) const;
  130.       String operator+(const char*) const;
  131.       String operator+(char C) const;
  132. //    friend String operator+(const StackString&, const String&);
  133.       friend String operator+(const String40&, const String&);
  134.       friend String operator+(const String120&, const String&);
  135.       friend String operator+(const char* CS, const String& S);
  136.       friend String operator+(char C, const String& S);
  137.  
  138.       // Appending stuff to a String
  139.       void operator+=(const BaseString&);
  140.       void operator+=(const char*);
  141.       void operator+=(char);
  142.  
  143.       void Left(int NewSize);  //  resize string and left justify text
  144.       void Right(int NewSize);
  145.       void Center(int NewSize);
  146.       void Just(int Type, int NewSize);
  147.         //  parses out to Left, Right or Center
  148.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  149.  
  150.       int ReAlloc(int NewCapacity);
  151.       String At(int Index, int Length) const;
  152.       String operator()(int Index, int Length) const {return At(Index,Length);}
  153.       char   At(int Index) const {return BaseString::At(Index);}
  154.       char   operator()(int Index) const {return BaseString::At(Index);}
  155.       String Before(int Index) const {return At(0,Index);}
  156.       String Through(int Index) const {return At(0,Index+1);}
  157.       String From(int Index) const {return At(Index,Len-Index);}
  158.       String After(int Index) const {return At(Index+1,Len-Index-1);}
  159.  
  160.       void Insert(char C,int Index=0);
  161.       void Insert(const char* St,int Index=0);
  162.   };
  163.  
  164. class StackString: public BaseString
  165.   {
  166.     protected:
  167.       friend String;
  168.     public:
  169.  
  170.       void operator=(const BaseString&);
  171.       void operator=(const char*);
  172.       void operator=(const char);
  173.  
  174.       void operator+=(const BaseString&);
  175.       void operator+=(const char*);
  176.       void operator+=(char);
  177.  
  178.       void Left(int NewSize);  //  resize string and left justify text
  179.       void Right(int NewSize);
  180.       void Center(int NewSize);
  181.       void Just(int Type, int NewSize);
  182.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  183.  
  184.       void Sub(StackString& D, int Index, int Length);
  185.  
  186.       void Insert(char C,int Index=0);
  187.       void Insert(const char* St,int Index=0);
  188.   };
  189.  
  190. class String40: public StackString
  191.   {
  192.     protected:
  193.       char Buf[41];
  194.       friend String;
  195.     public:
  196.       String40();
  197.       String40(const char& C, int L=1);
  198.       String40(const char*);
  199.       String40(const BaseString&);
  200.  
  201.       String40 operator+(const String40& S) const;
  202.       String40 operator+(const char* S) const;
  203.       String40 operator+(char C) const;
  204.       friend String40 operator+(const char* S1, const String40& S2);
  205.       friend String40 operator+(char C, const String40& S);
  206.  
  207.       String40 At(int Index, int Length) const;
  208.       String40 operator()(int Index, int Length) const {return At(Index,Length);}
  209.       char   At(int Index) const {return BaseString::At(Index);}
  210.       char   op